﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data.Stats
{
  public class AverageFunction : StatFunction
  {
    public AverageFunction()
      : base()
    {
    }

    public AverageFunction( string resultPropertyName, string sourcePropertyName )
      : base( resultPropertyName, sourcePropertyName )
    {
    }

    #region RequiresAccumulation Property

    protected internal override bool RequiresAccumulation
    {
      get
      {
        return false;
      }
    }

    #endregion

    #region PrerequisiteFunctions Property

    protected internal override StatFunction[] PrerequisiteFunctions
    {
      get
      {
        StatFunction[] prerequisites = m_prerequisites;

        if( prerequisites == null )
        {
          prerequisites = new StatFunction[]
          {
            new SumFunction( "AverageFunction.Sum", this.SourcePropertyName ),
            new CountFunction( "AverageFunction.Count", this.SourcePropertyName )
          };

          if( this.IsSealed )
            m_prerequisites = prerequisites;
        }

        return prerequisites;
      }
    }

    #endregion

    protected internal override void Validate()
    {
      base.Validate();
      this.ValidateSourcePropertyName( 1 );
    }

    protected override void Initialize( Type[] sourcePropertyTypes )
    {
      base.Initialize( sourcePropertyTypes );

      switch( Type.GetTypeCode( sourcePropertyTypes[ 0 ] ) )
      {
        case TypeCode.Char:
        case TypeCode.Byte:
        case TypeCode.SByte:
        case TypeCode.Int16:
        case TypeCode.Int32:
        case TypeCode.Int64:
        case TypeCode.UInt16:
        case TypeCode.UInt32:
        case TypeCode.UInt64:
        case TypeCode.Single:
        case TypeCode.Double:
          m_resultType = TypeCode.Double;
          break;

        case TypeCode.Decimal:
          m_resultType = TypeCode.Decimal;
          break;
      }
    }

    protected internal override void InitializePrerequisites( StatResult[] prerequisitesValues )
    {
      if( prerequisitesValues.Length != 2 )
        throw new InvalidOperationException( "The prerequisites initializers do not match the PrerequisiteFunctions." );

      m_sum = prerequisitesValues[ 0 ].Value;
      m_count = prerequisitesValues[ 1 ].Value;
    }

    protected internal override void Reset()
    {
      m_sum = null;
      m_count = null;
    }

    protected internal override void Accumulate( object[] values )
    {
      Debug.Assert( false, "Prerequisites are sufficient. Should not be called." );
    }

    protected internal override StatResult GetResult()
    {
      object result = null;

      switch( m_resultType )
      {
        case TypeCode.Double:
          {
            double count = Convert.ToDouble( m_count );

            if( count == 0d )
              throw new DivideByZeroException();

            result = Convert.ToDouble( m_sum ) / count;
          }
          break;

        case TypeCode.Decimal:
          {
            decimal count = Convert.ToDecimal( m_count );

            if( count == 0m )
              throw new DivideByZeroException();

            result = Convert.ToDecimal( m_sum ) / count;
          }
          break;
      }

      return new StatResult( result );
    }

    private object m_count;
    private object m_sum;
    private StatFunction[] m_prerequisites;
    private TypeCode m_resultType = TypeCode.Empty;
  }
}
